package com.animal.controller;


import com.animal.auto.JSONAuthentication;
import com.animal.entity.AnAnimal;
import com.animal.entity.AnComment;
import com.animal.mapper.AnCollectMapper;
import com.animal.mapper.AnCommentMapper;
import com.animal.response.Result;
import com.animal.service.AnCommentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author animal
 * @since 2022-01-29
 */
@RestController
@RequestMapping("/an-comment")
public class AnCommentController extends JSONAuthentication {
    @Autowired
    AnCommentMapper anCommentMapper;
    @Autowired
    AnCommentService anCommentService;
    @ApiOperation(value = "添加评论")
    @PostMapping("/add")
    public void addCommnet(HttpServletRequest request, HttpServletResponse response, @RequestBody AnComment anComment) throws  Exception{
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        anComment.setCoCreatTime(timestamp);

        int i=anCommentMapper.insert(anComment);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "删除评论")
    @PostMapping("/del")
    public void delComment(HttpServletRequest request, HttpServletResponse response, @RequestParam("coId") int coId ) throws Exception{
        int i=anCommentMapper.deleteById(coId);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "修改评论")
    @PostMapping("/upd")
    public void updAnimal(HttpServletRequest request, HttpServletResponse response, @RequestBody AnComment anComment)throws Exception{
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        anComment.setCoUpdTime(timestamp);

        int i=anCommentMapper.updateById(anComment);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "查询评论")
    @PostMapping("/look")
    public void lookAnimal(HttpServletRequest request, HttpServletResponse response,@RequestParam("current") int current,@RequestParam("size") int size)throws  Exception{
        int total=anCommentMapper.selectCount(null);
        List<AnComment> list =anCommentService.findAll(size,current);
        Result ok = Result.ok().data("AnComment",list);
        ok.data("total",total);
        this.WriteJSON(request, response, ok);
    }
    @ApiOperation(value = "查询评论通过用户id")
    @PostMapping("/lookByUserId")
    public void lookAnimalByUserId(HttpServletRequest request, HttpServletResponse response,@RequestParam("current") int current,@RequestParam("size") int size,@RequestParam("userId") int userId)throws  Exception{
        QueryWrapper<AnComment> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        int total=anCommentMapper.selectCount(wrapper);
        List<AnComment> list =anCommentService.findByUserId(size,current,userId);
        Result ok = Result.ok().data("AnComment",list);
        ok.data("total",total);
        this.WriteJSON(request, response, ok);
    }
    @ApiOperation(value = "查询评论通过商品id")
    @PostMapping("/lookByPrId")
    public void lookAnimalByUserIda(HttpServletRequest request, HttpServletResponse response,@RequestParam("current") int current,@RequestParam("size") int size,@RequestParam("prId") int prId)throws  Exception{
        QueryWrapper<AnComment> wrapper = new QueryWrapper<>();
        wrapper.eq("pr_id",prId);
        int total=anCommentMapper.selectCount(wrapper);
        List<AnComment> list =anCommentService.findByPrId(current,size,prId);
        Result ok = Result.ok().data("AnComment",list);
        ok.data("total",total);
        this.WriteJSON(request, response, ok);
    }
    @ApiOperation(value = "查询评论通过动物id")
    @PostMapping("/lookByAnId")
    public void lookAnimalByUserIdab(HttpServletRequest request, HttpServletResponse response,@RequestParam("current") int current,@RequestParam("size") int size,@RequestParam("anId") int anId)throws  Exception{
        QueryWrapper<AnComment> wrapper = new QueryWrapper<>();
        wrapper.eq("an_id",anId);
        int total=anCommentMapper.selectCount(wrapper);
        List<AnComment> list =anCommentService.findByAnId(current,size,anId);
        Result ok = Result.ok().data("AnComment",list);
        ok.data("total",total);
        this.WriteJSON(request, response, ok);
    }
    @ApiOperation(value = "查询评论通过帖子id")
    @PostMapping("/lookByInId")
    public void lookAnimalByUserIdabc(HttpServletRequest request, HttpServletResponse response,@RequestParam("current") int current,@RequestParam("size") int size,@RequestParam("inId") int inId)throws  Exception{
        QueryWrapper<AnComment> wrapper = new QueryWrapper<>();
        wrapper.eq("in_id",inId);
        int total=anCommentMapper.selectCount(wrapper);
        List<AnComment> list =anCommentService.findByInId(current,size,inId);
        Result ok = Result.ok().data("AnComment",list);
        ok.data("total",total);
        this.WriteJSON(request, response, ok);
    }

}

